/*
 * bsp_app.h
 *
 * Copyright (C) 2011 - Dr.NP
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @version bsp_app.h 1.0.0
 * @package libbspapp
 * @author Dr.NP <np@bsgroup.org>
 * @update 05/18/2011
 */

#ifndef BSP_APP_H

#define BSP_APP_H                               1

#include "bsp_utils.h"
#include "bsp_socket.h"
#include "bsp_script.h"

#include "lua.h"
#include "lauxlib.h"

// Definations
#define APP_PROP_EXT_LIST                       "ext_list"
#define APP_PROP_APP_LIST                       "app_list"
#define APP_PROP_SUB_APPNAME                    "appname"
#define APP_PROP_SUB_TRANSPORT                  "transport"
#define APP_PROP_SUB_ADDR                       "addr"
#define APP_PROP_SUB_PORT                       "port"
#define APP_PROP_SUB_EPOLL                      "epoll"
#define APP_PROP_SUB_NET                        "net"
#define APP_PROP_SUB_ON_CONN                    "on_conn"
#define APP_PROP_SUB_ON_CLOSE                   "on_close"
#define APP_PROP_SUB_ON_DATA                    "on_data"
#define APP_PROP_SUB_SCRIPT                     "script"

#define MAX_APP_NAME_LENGTH                     128
#define APP_LIST_INITIAL                        1024
#define MAX_API_GROUPS                          1024

#define DEFAULT_EXT_BASEDIR                     "./"
#define DEFAULT_SCRIPT_BASEDIR                  "./"

// Macros

// Structs
struct bsp_ext_func_t
{
    char lua_func[MAX_FUNC_NAME_LENGTH];
    char symbol[MAX_FUNC_NAME_LENGTH];
};

struct bsp_ext_entry_t
{
    void (* on_load) (int);
    void (* on_unload) (int);
    void (* on_conn) (int);
    void (* on_close) (int);
    size_t (* on_data) (int, char *, size_t);
    void (* on_error) (int);
};

struct bsp_ext_api_t
{
    int (* func) (lua_State *);
    const char *lua_func;
};

typedef struct bsp_extension_t
{
    int ext_id;
    char * ext_name;
    struct bsp_ext_entry_t *entry;
    struct bsp_ext_api_t *api;
} BSP_EXTENSION;

struct app_t
{
    int app_id;
    int srv_id;
    int script_id;

    int port;
    int transport;
    int net;
    int epoll;
    int chk_heartbeat;
    
    char appname[MAX_APP_NAME_LENGTH];
    char filename[_POSIX_PATH_MAX];
    char addr[ADDR_DSP_LENGTH];

    struct bsp_ext_entry_t *entry;
    char *apis[MAX_API_GROUPS];
    int napis;
    
    char luascript[_POSIX_PATH_MAX];

    char in_use;
    char is_running;
};

// Functions
int app_init(char *);
int new_app();
int free_app(int);
struct app_t * get_app(int);

int new_ext();
struct bsp_extension_t * get_ext(int);
struct bsp_extension_t * get_ext_by_name(const char *);

#endif  /* bsp_app.h */
